﻿/******************************************************************************
 *  All Rights Reserved , Copyright (C) 2012 , EricHu. 
 *  作    者： EricHu
 *  创建时间： 2012-5-10
 ******************************************************************************/
using System;
using System.Data;
using System.Reflection;

namespace RDIFramework.ServiceAdapter
{
    using RDIFramework.BusinessEntity;
    using RDIFramework.IService;
    using RDIFramework.Utilities;

    /// <summary>
    /// PermissionService
    /// 权限判断服务
    /// 
    /// 修改记录
    /// 
    ///		2012-06-12 版本：1.0 EricHu 对权限服务进行重构。
    ///		
    /// 版本：1.0
    ///
    /// <author>
    ///		<name>EricHu</name>
    ///		<date>2012-05-12</date>
    /// </author> 
    /// </summary>
    public partial class PermissionService : System.MarshalByRefObject, IPermissionService
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////////
        /// 用户权限范围判断相关
        /// 按某个权限范围获取特定用户可访问的特定资源（这个权限范围主要指数据权限【如：资源管理权限、资源访问权限等】）
        //////////////////////////////////////////////////////////////////////////////////////////////////////

        #region public DataTable GetUserDTByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode) 按某个权限范围获取特定用户可访问的用户列表
        /// <summary>
        /// 按某个权限范围获取特定用户可访问的用户列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemCode">操作权限编号</param>
        /// <returns>数据表</returns>
        public DataTable GetUserDTByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode)
        {
            // 写入调试信息
#if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif

            // 加强安全验证防止未授权匿名调用
#if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
#endif

            DataTable dataTable = new DataTable(PiUserTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 获得组织机构列表
                    PiPermissionScopeManager permissionScopeManager = new PiPermissionScopeManager(dbProvider, userInfo);
                    // 若权限是空的，直接返回所有数据
                    if (!String.IsNullOrEmpty(permissionItemCode))
                    {
                        dataTable = permissionScopeManager.GetUserDT(userInfo.Id, permissionItemCode);
                    }
                    dataTable.TableName = PiUserTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionService_GetUserDTByPermissionScope, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
#if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif

            return dataTable;
        }
        #endregion

        #region public string[] GetUserIdsByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode) 按某个权限范围获取特定用户可访问的用户主键数组
        /// <summary>
        /// 按某个权限范围获取特定用户可访问的用户主键数组
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemCode">操作权限编号</param>
        /// <returns>主键数组</returns>
        public string[] GetUserIdsByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode)
        {
            // 写入调试信息
#if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif

            // 加强安全验证防止未授权匿名调用
#if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
#endif

            string[] returnValue = null;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 若权限是空的，直接返回所有数据
                    if (String.IsNullOrEmpty(permissionItemCode))
                    {
                        return returnValue;
                    }
                    // 获得用户主键数组
                    PiPermissionScopeManager permissionScopeManager = new PiPermissionScopeManager(dbProvider, userInfo);
                    returnValue = permissionScopeManager.GetUserIds(userId, permissionItemCode);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionService_GetUserIdsByPermissionScope, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
#if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif

            return returnValue;
        }
        #endregion
        
        #region public DataTable GetRoleDTByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode) 按某个权限范围获取特定用户可访问的取角色列表
        /// <summary>
        /// 按某个权限范围获取特定用户可访问的取角色列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemCode">操作权限编号</param>
        /// <returns>数据表</returns>
        public DataTable GetRoleDTByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            DataTable dataTable = new DataTable(PiRoleTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 若权限是空的，直接返回所有数据
                    if (userInfo.IsAdministrator || String.IsNullOrEmpty(permissionItemCode))
                    {
                        // 获得角色列表
                        PiRoleManager roleManager = new PiRoleManager(dbProvider, userInfo);
                        dataTable = roleManager.GetDT(PiRoleTable.FieldDeleteMark, 0, PiRoleTable.FieldSortCode);
                    }
                    else
                    {
                        // 获得组织机构列表
                        PiPermissionScopeManager permissionScopeManager = new PiPermissionScopeManager(dbProvider, userInfo);
                        dataTable = permissionScopeManager.GetRoleDT(userInfo.Id, permissionItemCode);
                    }
                    dataTable.TableName = PiRoleTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionService_GetRoleDTByPermissionScope, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return dataTable;
        }
        #endregion

        #region public string[] GetRoleIdsByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode) 按某个权限范围获取特定用户可访问的角色主键数组
        /// <summary>
        /// 按某个权限范围获取特定用户可访问的角色主键数组
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemCode">操作权限编号</param>
        /// <returns>主键数组</returns>
        public string[] GetRoleIdsByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            string[] returnValue = null;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 若权限是空的，直接返回所有数据
                    if (String.IsNullOrEmpty(permissionItemCode))
                    {
                        return returnValue;
                    }
                    // 获得角色主键数组
                    PiPermissionScopeManager permissionScopeManager = new PiPermissionScopeManager(dbProvider, userInfo);
                    returnValue = permissionScopeManager.GetRoleIds(userId, permissionItemCode);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionService_GetRoleIdsByPermissionScope, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
            #if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
            #endif

            return returnValue;
        }
        #endregion
   
        #region public DataTable GetModuleDTByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode) 按某个权限范围获取特定用户可访问的模块列表
        /// <summary>
        /// 按某个权限范围获取特定用户可访问的模块列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemCode">权限域编号</param>
        /// <returns>数据表</returns>
        public DataTable GetModuleDTByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            DataTable dataTable = new DataTable(PiModuleTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiModuleManager moduleManager = new PiModuleManager(dbProvider, userInfo);
                    dataTable = moduleManager.GetDTByPermission(userId, permissionItemCode);
                    dataTable.TableName = PiModuleTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionService_GetModuleDTByPermissionScope, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
#if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif

            return dataTable;
        }
        #endregion

        #region public DataTable GetPermissionItemDTByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode) 按某个权限范围获取特定用户可访问的操作权限列表(有授权权限的权限列表)
        /// <summary>
        /// 按某个权限范围获取特定用户可访问的操作权限列表(有授权权限的权限列表)
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemCode">权限域编号</param>
        /// <returns>数据表</returns>
        public DataTable GetPermissionItemDTByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode)
        {
            // 写入调试信息
#if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif

            // 加强安全验证防止未授权匿名调用
#if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
#endif

            DataTable dataTable = new DataTable(PiPermissionItemTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    PiPermissionItemManager permissionItemManager = new PiPermissionItemManager(dbProvider, userInfo);
                    string permissionItemId = permissionItemManager.GetId(PiPermissionItemTable.FieldCode, permissionItemCode);
                    // 数据库里没有设置可授权的权限项，系统自动增加一个权限配置项
                    if (String.IsNullOrEmpty(permissionItemId) && permissionItemCode.Equals("Resource.ManagePermission"))
                    {
                        PiPermissionItemEntity permissionItemEntity = new PiPermissionItemEntity();
                        permissionItemEntity.Code = "Resource.ManagePermission";
                        permissionItemEntity.FullName = "资源管理范围权限（系统默认）";
                        permissionItemEntity.IsScope = 1;
                        permissionItemEntity.Enabled = 1;
                        permissionItemEntity.AllowDelete = 0;
                        permissionItemEntity.AllowDelete = 0;
                        permissionItemManager.AddEntity(permissionItemEntity);
                    }
                    dataTable = permissionItemManager.GetDTByUser(userId, permissionItemCode);
                    dataTable.TableName = PiPermissionItemTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionService_GetPermissionItemDTByPermissionScope, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
#if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif

            return dataTable;
        }
        #endregion

        #region public DataTable GetOrganizeDTByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode) 按某个权限范围获取特定用户可访问的组织机构列表
        /// <summary>
        /// 按某个权限范围获取特定用户可访问的组织机构列表
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemCode">操作权限编号</param>
        /// <returns>数据表</returns>
        public DataTable GetOrganizeDTByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode)
        {
            // 写入调试信息
            #if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
            #endif

            // 加强安全验证防止未授权匿名调用
            #if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
            #endif

            DataTable dataTable = new DataTable(PiOrganizeTable.TableName);
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);

                    // 若权限是空的，直接返回所有数据
                    if (String.IsNullOrEmpty(permissionItemCode))
                    {
                        PiOrganizeManager organizeManager = new PiOrganizeManager(dbProvider, userInfo);
                        dataTable = organizeManager.GetDT();
                        dataTable.DefaultView.Sort = PiOrganizeTable.FieldSortCode;
                    }
                    else
                    {
                        //获得组织机构列表
                        PiPermissionScopeManager permissionScopeManager = new PiPermissionScopeManager(dbProvider, userInfo);
                        if (string.IsNullOrEmpty(userId))
                        {
                            dataTable = permissionScopeManager.GetOrganizeDT(userInfo.Id, permissionItemCode);
                        }
                        else
                        {
                            dataTable = permissionScopeManager.GetOrganizeDT(userId, permissionItemCode);
                        }

                        dataTable.DefaultView.Sort = PiOrganizeTable.FieldSortCode;
                    }
                    dataTable.TableName = PiOrganizeTable.TableName;
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionService_GetOrganizeDTByPermissionScope, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
#if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif

            return dataTable;
        }
        #endregion

        #region public string[] GetOrganizeIdsByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode) 按某个权限范围获取特定用户可访问的组织机构主键数组
        /// <summary>
        /// 按某个权限范围获取特定用户可访问的组织机构主键数组
        /// </summary>
        /// <param name="userInfo">用户</param>
        /// <param name="userId">用户主键</param>
        /// <param name="permissionItemCode">操作权限编号</param>
        /// <returns>主键数组</returns>
        public string[] GetOrganizeIdsByPermissionScope(UserInfo userInfo, string userId, string permissionItemCode)
        {
            // 写入调试信息
#if (DEBUG)
            int milliStart = BusinessLogic.StartDebug(userInfo, MethodBase.GetCurrentMethod());
#endif

            // 加强安全验证防止未授权匿名调用
#if (!DEBUG)
                LogOnService.UserIsLogOn(userInfo);
#endif

            string[] returnValue = null;
            using (IDbProvider dbProvider = DbFactoryProvider.GetProvider(SystemInfo.RDIFrameworkDbType))
            {
                try
                {
                    dbProvider.Open(RDIFrameworkDbConection);
                    // 若权限是空的，直接返回所有数据
                    if (String.IsNullOrEmpty(permissionItemCode))
                    {
                        return returnValue;
                    }
                    // 获得组织机构列表
                    PiPermissionScopeManager permissionScopeManager = new PiPermissionScopeManager(dbProvider, userInfo);
                    returnValue = permissionScopeManager.GetOrganizeIds(userId, permissionItemCode);
                    LogManager.Instance.Add(dbProvider, userInfo, this.serviceName, RDIFrameworkMessage.PermissionService_GetOrganizeIdsByPermissionScope, MethodBase.GetCurrentMethod());
                }
                catch (Exception ex)
                {
                    CiExceptionManager.LogException(dbProvider, userInfo, ex);
                    throw ex;
                }
                finally
                {
                    dbProvider.Close();
                }
            }

            // 写入调试信息
#if (DEBUG)
            BusinessLogic.EndDebug(MethodBase.GetCurrentMethod(), milliStart);
#endif

            return returnValue;
        }
        #endregion

    }
}
